#line 1 "C:/Users/Waroflords/Desktop/Working now PMA/PWM22June[Simple]/PWMTEST.c"
#line 1 "c:/users/waroflords/desktop/working now pma/pwm22june[simple]/tmrxconfig.h"
#line 30 "c:/users/waroflords/desktop/working now pma/pwm22june[simple]/tmrxconfig.h"
 void Timer0_Init();
 void Timer0_IE();
 void Timer0_ID();
 void Timer0_OptionReg(int value);

 void Timer1_Init();
 void Timer1_IE();
 void Timer1_ID();
 void Timer1_SetPeriod(unsigned short int H_Value, unsigned short int L_Value);
 void Timer1_Start();
 void Timer1_Stop();
 void Timer1_T1CON(int value);

 void Timer2_Init();
 void Timer2_Start();
 void Timer2_Stop();
 void Timer2_Presc(unsigned short int preMode);
 void Timer2_IE();
 void Timer2_ID();

 void OSC_Osctune(int value);
 void OSC_Osccon(int value);
#line 63 "c:/users/waroflords/desktop/working now pma/pwm22june[simple]/tmrxconfig.h"
 void Timer0_IE()
 {
 INTCON.TMR0IE = 1;
 }

 void Timer0_ID()
 {
 INTCON.TMR0IE = 0;
 }


 void Timer0_OptionReg(unsigned short int value)
 {


 OPTION_REG = value;
 }

 void Timer1_Init()
 {


 T1CON &= ~TMR1CS;


 T1CON.T1RUN =0;


 T1CON.T1CKPS1 =1;
 T1CON.T1CKPS0 =1;

 PIR1.TMR1IF = 0;
 TMR1H = 0;
 TMR1L = 0;

 Timer1_IE();
#line 109 "c:/users/waroflords/desktop/working now pma/pwm22june[simple]/tmrxconfig.h"
 }

 void Timer1_IE()
 {
 PIE1.TMR1IE = 1;
 }

 void Timer1_ID()
 {
 PIE1.TMR1IE = 0;
 }

 void Timer1_SetPeriod(unsigned short int H_Value, unsigned short int L_Value)
 {


 if(T1CON.TMR1ON =1)
 {
 Timer1_Stop();
 TMR1L = L_Value;
 TMR1H = H_Value;
 Timer1_Start();
 }
 else
 {
 Timer1_Stop();
 TMR1L = L_Value;
 TMR1H = H_Value;
 }

 }
 void Timer1_Start()
 {
 PIR1.TMR1IF = 0;
 T1CON.TMR1ON =1;
 }

 void Timer1_Stop()
 {
 T1CON.TMR1ON =0;
 }

 void Timer1_T1CON(unsigned short int value)
 {

 T1CON = value;
 }


 void Timer2_Init()
 {


 }

 void Timer2_Start()
 {
 T2CON.TMR2ON =1;

 }
 void Timer2_Stop()
 {
 T2CON.TMR2ON =0;

 }

 void Timer2_Presc(unsigned short int preMode)
 {
 switch(preMode)
 {
 case 0:
 T2CON &= ~0b00000011;
 break;
 case 1:
 T2CON.T2CKPS0=1;
 T2CON.T2CKPS1=0;
 break;
 case 2:
 T2CON.T2CKPS1 =1;
 break;

 }
 }
 void Timer2_IE()
 {
 PIE1.TMR2ON =1;
 }

 void Timer2_ID()
 {
 PIE1.TMR2ON =0;
 }

 void OSC_Osctune(unsigned short int value)
 {
 OSCTUNE = value;
 }
 void OSC_Osccon(unsigned short int value)
 {
 OSCCON = value;
 }
#line 1 "c:/users/waroflords/desktop/working now pma/pwm22june[simple]/config.h"
#line 1 "c:/users/waroflords/desktop/working now pma/pwm22june[simple]/sine.h"
#line 1 "c:/users/waroflords/desktop/working now pma/pwm22june[simple]/sinetablegenerator.h"
#line 20 "c:/users/waroflords/desktop/working now pma/pwm22june[simple]/sinetablegenerator.h"
double getSine(unsigned int intDeg){

 int sinvalue =SinE3(intDeg);
 return((double)sinvalue/1000);
}
#line 39 "c:/users/waroflords/desktop/working now pma/pwm22june[simple]/sinetablegenerator.h"
int generateTable(unsigned short int *ptrStart, int size){
 int x;
 unsigned short int *ptrCurrent;
 if((size%12)!=0){return(0);}

 ptrCurrent = ptrStart;

 for(x=0; x < size; x++){

 *(ptrCurrent++) = (int) (getSine((x)*(360/size))* 127 + 128 );
 }

 return(1);
}
#line 13 "c:/users/waroflords/desktop/working now pma/pwm22june[simple]/sine.h"
unsigned short int sineValues[ ( 9 *4) ];

unsigned short int sinetableInit(){
 if(!generateTable(&sineValues[0],  ( 9 *4) )){
 return(0);
 }
}
#line 73 "C:/Users/Waroflords/Desktop/Working now PMA/PWM22June[Simple]/PWMTEST.c"
enum States {
DRIVING,
BREAKING,
STANDSTILL,
ACC
}programState=ACC;



 unsigned short iSpeed=0,BreakON= 0 , tmr1Overflow= 0 ,tmr0Overflow= 0 , pwmOn= 0 ,timerValue =0;
 int n_position = 0;
 int n2_position=0,n3_position=0;
 unsigned short int PWMSignal[3];
 double tPeriod,tcp,t1Freq, t0Freq = 7812.50, acceleration=10.00;
 double currentSpeed =  5 , newSpeed =  43 ,unitstep, unitTime ;
 unsigned int h_value=0, TIMER1V=0, iDirection=0;
 unsigned short int getFreqInADC(double);

 unsigned short int l_value=0, newDirection= 0 ,currentDirection =  0 ;


 void sineWave(double);
 void portInit();
void checkControlPanel();
void updateDutyCycle(unsigned short int , unsigned short int);
double getFreqInHz(unsigned short , double ) ;
void calcUnitTime(double , double );

void initMain() {

 portInit();

 sinetableInit();

 Pwm1_Init(5000);
 Pwm2_Init(5000);
 Pwm3_Init(5000);
 Timer1_Init();

 Timer0_OptionReg(0x87);
 calcUnitTime(acceleration,t0Freq);
 Timer0_IE();
 TMR0 = timerValue;

 INTCON.GIE =1;
 INTCON.PEIE =1;
 }

void portInit()
{
#line 131 "C:/Users/Waroflords/Desktop/Working now PMA/PWM22June[Simple]/PWMTEST.c"
 TRISA = 0b11110111;
 PORTA = 0x00;



 TRISB = 0b00000000;
 PORTB = 0x00;

 TRISC = 0b10111001;
 PORTC = 0x00;
}

void main() {
 ADCON1 = 0b10001101;
 initMain();

 while (1)
 {

 switch (programState)
 {

 case ACC:
#line 161 "C:/Users/Waroflords/Desktop/Working now PMA/PWM22June[Simple]/PWMTEST.c"
  PORTB.F1  =0;
  PORTB.F2  =1;
  PORTB.F3  =1;
  PORTB.F4  =0;
  PORTB.F6  =0;

  PORTB.F7  =0;

 if(pwmON== 0 )
 {
 Pwm1_Start();
 Pwm2_Start();
 Pwm3_Start();
 Timer1_IE() ;
 Timer1_Start();
 pwmON= 1 ;
 }



 if(tmr0Overflow == 1 )
 {

 if(currentSpeed<newSpeed)
 currentSpeed = currentSpeed+unitStep;

 sineWave(currentSpeed);
 iSpeed= getFreqInADC(currentSpeed);
 tmr0Overflow = 0 ;
 Timer0_IE();
 }


 if(currentSpeed >=newSpeed)
 {
 programState=DRIVING;
 Timer0_ID();
 }


 break;
 case DRIVING:
#line 209 "C:/Users/Waroflords/Desktop/Working now PMA/PWM22June[Simple]/PWMTEST.c"
 checkControlPanel();


  PORTB.F1  =1;
  PORTB.F2  =0;
  PORTB.F3  =1;
  PORTB.F4  =0;
  PORTB.F6  =0;

  PORTB.F7  =0;




 if(pwmON== 0 )
 {
 Pwm1_Start();
 Pwm2_Start();
 Pwm3_Start();
 Timer1_IE() ;
 Timer1_Start();
 pwmON= 1 ;
 }


 if(BreakON ==  1 )
 {
 programState=BREAKING;
 newSpeed=  5 ;
 Timer0_IE();
 }
 break;
 case BREAKING :
#line 249 "C:/Users/Waroflords/Desktop/Working now PMA/PWM22June[Simple]/PWMTEST.c"
  PORTB.F1  =0;
  PORTB.F2  =1;
  PORTB.F3  =1;
  PORTB.F4  =1;
  PORTB.F6  =1;

  PORTB.F7  =1;

 if(pwmON== 0 )
 {
 Pwm1_Start();
 Pwm2_Start();
 Pwm3_Start();
 Timer1_IE() ;
 Timer1_Start();
 pwmON= 1 ;
 }


 checkControlPanel();



 if(tmr0Overflow == 1 )
 {

 if(currentSpeed>newSpeed)
 currentSpeed = currentSpeed-unitStep;

 sineWave(currentSpeed);
 iSpeed= getFreqInADC(currentSpeed);
 tmr0Overflow = 0 ;
 Timer0_IE();
 }
#line 291 "C:/Users/Waroflords/Desktop/Working now PMA/PWM22June[Simple]/PWMTEST.c"
 if(currentSpeed <=newSpeed)
 {
 programState=STANDSTILL;
  PORTB.F7  =0;
 }


 break;

 case STANDSTILL:
#line 307 "C:/Users/Waroflords/Desktop/Working now PMA/PWM22June[Simple]/PWMTEST.c"
 checkControlPanel();


  PORTB.F1  =1;
  PORTB.F2  =1;
  PORTB.F3  =1;
  PORTB.F4  =1;
  PORTB.F6  =1;

  PORTB.F7  =0;

 if(pwmON== 0 )
 {
 Pwm1_Start();
 Pwm2_Start();
 Pwm3_Start();
 Timer1_IE() ;
 Timer1_Start();
 pwmON= 1 ;
 }




 if(BreakON ==  0 )
 {
 programState=ACC;

 currentSpeed =  5 ;
 newSpeed=  43 ;
 Timer0_IE();
 }
 break;
 default:


  PORTB.F1  =0;
  PORTB.F2  =0;
  PORTB.F3  =0;
  PORTB.F4  =0;
  PORTB.F6  =0;
 break;
 }

}
}
void calcUnitTime(double a, double tmr0Freq )
{
 double tmp=0;
 unitstep =0.25;
 unitTime = unitstep/a;
 tmp = unitTime*tmr0Freq;
 timerValue = 0xFF - tmp;
}

void checkControlPanel()
{





 iDirection = Adc_Read(1);
 BreakON = (iDirection>255) ? 1  : 0 ;


}

double getFreqInHz(unsigned short adcSpeed, double MAXFREQ)
{
 double freq=0;
 double ratioFreq= (255.00)/MAXFREQ;

 if(adcSpeed ==255.00)
 freq = MAXFREQ;
 else
 freq= (double)adcSpeed/ratioFreq;

 return freq;

}

unsigned short int getFreqInADC(double Speed)
{
 unsigned short int adcfreq=0;
 double ratioFreq= (43.00)/255;

 if(Speed >=43.00)
 adcfreq = 255;
 else
 adcfreq= (double)Speed/ratioFreq;

 return adcfreq;

}

void sineWave(double frequency)
{
 unsigned short int tmr1Prescale=8;



 tPeriod = (double)1/ frequency;
 tcp = tPeriod/ ( ( 9 *4) );
 t1Freq = (double) 8000000 /(4*tmr1Prescale);
 TIMER1V = (65535)-(tcp*t1Freq);
 l_value= (unsigned short int)TIMER1V & 0x00FF;
 h_value = (TIMER1V & 0xFF00)>>8;
}



void updateDutyCycle(unsigned short int dir, unsigned short freq)
{
 unsigned short int temp;
#line 434 "C:/Users/Waroflords/Desktop/Working now PMA/PWM22June[Simple]/PWMTEST.c"
 switch(dir)
 {
 case  1 :

 n_position++;
 n_position = (n_position>= ( 9 *4) ) ?(n_position- ( 9 *4) ) :n_position ;

 Pwm1_Change_Duty((PWMSignal[0]*freq)>>8);
 Pwm2_Change_Duty((PWMSignal[1]*freq)>>8);
 Pwm3_Change_Duty((PWMSignal[2]*freq)>>8);
 break;

 case  2 :


 temp = PWMSignal[0];
 PWMSignal[0] = PWMSignal[1];
 PWMSignal[1] = temp;

 Pwm1_Change_Duty((PWMSignal[0]*freq)>>8);
 Pwm2_Change_Duty((PWMSignal[1]*freq)>>8);
 Pwm3_Change_Duty((PWMSignal[2]*freq)>>8);
 break;

 default:
 Pwm1_Change_Duty(0);
 Pwm2_Change_Duty(0);
 Pwm3_Change_Duty(0);
 break;
 }

}


 void interrupt()
 {
 unsigned int temp=0,temp2=(PR2+1) ;

 if(PIR1.TMR1IF)
 {

  PORTB.F0  = ~( PORTB.F0 );

 TMR1H = h_value;
 TMR1L = l_value;


 n2_position = n_position+ ( ( 9 *4) /3); ;
 n2_position = (n2_position >= ( 9 *4) ) ?(n2_position- ( 9 *4) ) :n2_position ;
 n3_position = n2_position+ ( ( 9 *4) /3); ;
 n3_position = (n3_position >=  ( 9 *4) ) ?(n3_position- ( 9 *4) ) :n3_position ;

 temp =sineValues[n_position]*temp2;
 temp = temp>>6;

 CCPR1L = temp>>2;
 CCP1CON.CCP1Y = (temp&0x01) ?1 :0 ;
 CCP1CON.CCP1X = (temp&0x02) ?1 :0;



 temp =sineValues[n2_position]*temp2;
 temp = temp>>6;

 CCPR2L = temp>>2;
 CCP2CON.CCP2Y = (temp&0x01) ?1 :0 ;
 CCP2CON.CCP2X = (temp&0x02) ?1 :0;

 temp =sineValues[n3_position]*temp2;
 temp = temp>>6;

 CCPR3L = temp>>2;
 CCP3CON.CCP3Y = (temp&0x01) ?1 :0 ;
 CCP3CON.CCP3X = (temp&0x02) ?1 :0;
 n_position++;
 n_position = (n_position>= ( 9 *4) ) ?(n_position- ( 9 *4) ) :n_position ;



  PORTB.F0  = ~( PORTB.F0 );

 PIR1.TMR1IF =0;


 }else if(INTCON.TMR0IF)
 {
  PORTA.F3  = ~( PORTA.F3 );


 TMR0 =timerValue;
 INTCON.TMR0IF =0;
 Timer0_ID();
 tmr0Overflow = 1 ;
 }


 }
